<?php
/**
 * Page Template
 *
 * Loaded automatically by index.php?main_page=account.<br />
 * Displays previous orders and options to change various Customer Account settings
 *
 * @package templateSystem
 * @copyright Copyright 2003-2005 Zen Cart Development Team
 * @copyright Portions Copyright 2003 osCommerce
 * @license http://www.zen-cart.com/license/2_0.txt GNU Public License V2.0
 * @version $Id: tpl_account_default.php 4086 2006-08-07 02:06:18Z ajeh $
 * ALTER TABLE  `customers` ADD  `customers_public` BINARY( 1 ) NOT NULL DEFAULT  '0'
 */
function addColumnIfNotExist($col_name, $table_name, $db)
{
//    echo "ALTER TABLE " . $table_name . " ADD " . $col_name . " VARCHAR(255)";exit();
    $check = "SELECT " . $col_name . " FROM " . $table_name . " limit 1";

    if (false === mysql_query($check)) {
        $db->Execute("ALTER TABLE " . $table_name . " ADD " . $col_name . " VARCHAR(255)");
    }

}

function get_nickname_from_id($id)
{
    $sql = "SELECT customers_nickname FROM " . TABLE_CUSTOMERS . " WHERE customers_id='" . $id . "'";
    global $db;
    $result = $db->Execute($sql);
    return isset($result->fields) ? strtolower($result->fields['customers_nickname']) : null;
}

//addColumnIfNotExist("customers_nickname", TABLE_CUSTOMERS, $db);
if (isset($_POST["nick"])) {
    global $db;
    $check = "SELECT customers_nickname from " . TABLE_CUSTOMERS . " WHERE LOWER(customers_nickname)=LOWER('" . $_POST['nick'] . "')";
    $result = $db->Execute($check);
    if ($result->RecordCount() == 0) {
        $sql = "update " . TABLE_CUSTOMERS . " set customers_nickname = '" . $_POST['nick'] . "' where customers_id='" . $_SESSION['customer_id'] . "'";
        $db->Execute($sql);
        echo "<font color=red>Your nick name now is " . $_POST['nick'] . "</font>";
    } else {
        echo "<font color=red>This nick name (" . $_POST['nick'] . ") is not available for registration</font>";
    }


}
?>

<div class="centerColumn" id="accountDefault">

    <h1 id="accountDefaultHeading"><?php echo HEADING_TITLE; ?></h1>
    <?php if ($messageStack->size('account') > 0) echo $messageStack->output('account'); ?>

    <?php
    if (zen_count_customer_orders() > 0) {
        ?>
        <p class="forward"><?php echo '<a href="' . zen_href_link(FILENAME_ACCOUNT_HISTORY, '', 'SSL') . '">' . OVERVIEW_SHOW_ALL_ORDERS . '</a>'; ?></p>
        <br class="clearBoth"/>
        <table width="100%" border="0" cellpadding="0" cellspacing="0" id="prevOrders">
            <caption><h2><?php echo OVERVIEW_PREVIOUS_ORDERS; ?></h2></caption>
            <tr class="tableHeading">
                <th scope="col"><?php echo TABLE_HEADING_DATE; ?></th>
                <th scope="col"><?php echo TABLE_HEADING_ORDER_NUMBER; ?></th>
                <th scope="col"><?php echo TABLE_HEADING_SHIPPED_TO; ?></th>
                <th scope="col"><?php echo TABLE_HEADING_STATUS; ?></th>
                <th scope="col"><?php echo TABLE_HEADING_TOTAL; ?></th>
                <th scope="col"><?php echo TABLE_HEADING_VIEW; ?></th>
            </tr>
            <?php
            foreach ($ordersArray as $orders) {
                ?>
                <tr>
                    <td width="70px"><?php echo zen_date_short($orders['date_purchased']); ?></td>
                    <td width="30px"><?php echo TEXT_NUMBER_SYMBOL . $orders['orders_id']; ?></td>
                    <td>
                        <address><?php echo zen_output_string_protected($orders['order_name']) . '<br />' . $orders['order_country']; ?></address>
                    </td>
                    <td width="70px"><?php echo $orders['orders_status_name']; ?>
                    </td>
                    <td width="70px" align="right"><?php echo $orders['order_total']; ?></td>
                    <td align="right"><?php echo '<a href="' . zen_href_link(FILENAME_ACCOUNT_HISTORY_INFO, 'order_id=' . $orders['orders_id'], 'SSL') . '"> ' . zen_image_button(BUTTON_IMAGE_VIEW_SMALL, BUTTON_VIEW_SMALL_ALT) . '</a>'; ?></td>
                </tr>

            <?php
            }
            ?>
        </table>
    <?php
    }
    ?>
    <br class="clearBoth"/>

    <div id="accountLinksWrapper" class="back">
        <h2><?php echo MY_ACCOUNT_TITLE; ?></h2>
        <ul id="myAccountGen" class="list">
            <li><?php echo ' <a href="' . zen_href_link(FILENAME_ACCOUNT_EDIT, '', 'SSL') . '">' . MY_ACCOUNT_INFORMATION . '</a>'; ?></li>
            <li><?php echo ' <a href="' . zen_href_link(FILENAME_ADDRESS_BOOK, '', 'SSL') . '">' . MY_ACCOUNT_ADDRESS_BOOK . '</a>'; ?></li>
            <li><?php echo ' <a href="' . zen_href_link(FILENAME_ACCOUNT_PASSWORD, '', 'SSL') . '">' . MY_ACCOUNT_PASSWORD . '</a>'; ?></li>
            <?php

            $get_nickname_from_id = get_nickname_from_id($_SESSION['customer_id']);
            if ($get_nickname_from_id == null) {
                echo ' <li><a href="' .zen_href_link(FILENAME_PROFILE_CREATOR, '', 'SSL')  . '">' .
                    MY_ACCOUNT_PROFILE_CREATING . '</a></li>';
            } else {
                echo ' <li><a href="' . zen_href_link(FILENAME_PROFILE, 'nick=' . $get_nickname_from_id, 'SSL') . '">' .
                    MY_ACCOUNT_PROFILE_VIEWING . '</a></li>';
            }
            ?>
        </ul>


        <?php
        if (SHOW_NEWSLETTER_UNSUBSCRIBE_LINK != 'false' or CUSTOMERS_PRODUCTS_NOTIFICATION_STATUS != '0') {
            ?>
            <h2><?php echo EMAIL_NOTIFICATIONS_TITLE; ?></h2>
            <ul id="myAccountNotify" class="list">
                <?php
                if (SHOW_NEWSLETTER_UNSUBSCRIBE_LINK == 'true') {
                    ?>
                    <li><?php echo ' <a href="' . zen_href_link(FILENAME_ACCOUNT_NEWSLETTERS, '', 'SSL') . '">' . EMAIL_NOTIFICATIONS_NEWSLETTERS . '</a>'; ?></li>
                <?php } //endif newsletter unsubscribe ?>
                <?php
                if (CUSTOMERS_PRODUCTS_NOTIFICATION_STATUS == '1') {
                    ?>
                    <li><?php echo ' <a href="' . zen_href_link(FILENAME_ACCOUNT_NOTIFICATIONS, '', 'SSL') . '">' . EMAIL_NOTIFICATIONS_PRODUCTS . '</a>'; ?></li>

                <?php } //endif product notification ?>
            </ul>

        <?php } // endif don't show unsubscribe or notification ?>
    </div>

    <?php
    // only show when there is a GV balance
    if ($customer_has_gv_balance) {
        ?>
        <div id="sendSpendWrapper">
            <?php require($template->get_template_dir('tpl_modules_send_or_spend.php', DIR_WS_TEMPLATE, $current_page_base, 'templates') . '/tpl_modules_send_or_spend.php'); ?>
        </div>
    <?php
    }
    ?>
    <br class="clearBoth"/>

</div>